package com.example.springbootredis.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.springbootredis.entity.User;
import com.example.springbootredis.util.BaseResp;
import com.example.springbootredis.util.TestDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;


@Slf4j
@Api("redis【hash】")
@RestController
@RequestMapping("/redis/hash")
public class RedisHashController {

    @Autowired
    RedisTemplate redisTemplate;

    //-----------------redis hash start-------------
    String testHashKey = "hash_{}";

    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp hash_initData(){
        log.info("hash_initData start");
        for (User user : TestDataUtil.getUsers()) {
            log.info("hash_initData user:{}",user);
            redisTemplate.opsForHash().putAll(StrUtil.format(testHashKey,user.getId()),JSONUtil.parseObj(user));
        }
        log.info("hash_initData end");
        return BaseResp.buildSuccess();
    }
    @ApiOperation("put方法")
    @RequestMapping(value = "/put",method = RequestMethod.GET)
    public BaseResp hash_put(@RequestParam("h")String h,@RequestParam("hk")String hk,@RequestParam("hv")String hv){
        redisTemplate.opsForHash().put(h,hk,hv);
        return BaseResp.buildSuccess();
    }
    @ApiOperation("get方法")
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public BaseResp hash_get(@RequestParam("h")String h,@RequestParam("hk")String hk){
        redisTemplate.opsForHash().get(h,hk);
        return BaseResp.buildSuccess();
    }
    @ApiOperation("increment方法")
    @RequestMapping(value = "/increment",method = RequestMethod.GET)
    public BaseResp hash_increment(@RequestParam("h")String h,@RequestParam("hk")String hk){
        redisTemplate.opsForHash().increment(h,hk,1);
        return BaseResp.buildSuccess();
    }
    @ApiOperation("keys方法")
    @RequestMapping(value = "/keys",method = RequestMethod.GET)
    public BaseResp<Set> hash_keys(@RequestParam("h")String h){
        return BaseResp.buildDataSuccess(redisTemplate.opsForHash().keys(h));
    }

    @ApiOperation("keys方法")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public BaseResp<Long> delete(@RequestParam("h")String h){
        HashOperations hashOperations = redisTemplate.opsForHash();
        Long resp = hashOperations.delete(h);
        return BaseResp.buildDataSuccess(resp);
    }
    //-----------------redis hash end-------------

}
